//
//  CustomDatePickerView.m
//  YaoHeAPP
//
//  Created by 杨亚森 on 16/6/27.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CustomDatePickerView.h"

@interface CustomDatePickerView () <UIPickerViewDataSource, UIPickerViewDelegate> {
    NSString *_selectDateString;
}

@property (nonatomic, strong) UILabel *tipLabel;                    // 提示label
@property (nonatomic, strong) UIPickerView *pickerView;             // 自定义时间选择器
@property (nonatomic, strong) UIButton *cancelButton;               // 取消button
@property (nonatomic, strong) UIButton *conformButton;              // 确认button
@property (nonatomic, strong) NSArray *yearList;                    // 年数组
@property (nonatomic, strong) NSMutableArray *monthList;            // 月数组
@property (nonatomic, assign) NSInteger selectedYear;               // 选中的年数

@end

@implementation CustomDatePickerView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 提示label
        UILabel *tipLabel = [[UILabel alloc] init];
        tipLabel.backgroundColor = [UIColor blackColor];
        tipLabel.font = Font(14);
        tipLabel.textColor = [UIColor whiteColor];
        tipLabel.text = @"选择年月";
        tipLabel.textAlignment = NSTextAlignmentCenter;
        tipLabel.frame = CGRectMake(0, 0, self.width, 30);
        [self addSubview:tipLabel];
        self.tipLabel = tipLabel;
        
        // 时间选择器
        UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, tipLabel.bottom, self.width, 216)];
        [pickerView selectRow:0 inComponent:0 animated:YES];
        pickerView.delegate = self;
        pickerView.dataSource = self;
        [self addSubview:pickerView];
        self.pickerView = pickerView;
        
        UIView *firstLineView = [[UIView alloc] initWithFrame:CGRectMake(0, pickerView.bottom-5, self.width, 0.5)];
        firstLineView.backgroundColor = RGBCOLOR(230, 232, 241);
        [self addSubview:firstLineView];
        
        UIView *secondLineView = [[UIView alloc] initWithFrame:CGRectMake(self.width/2, pickerView.bottom-5, 0.5, 35)];
        secondLineView.backgroundColor = RGBCOLOR(230, 232, 241);
        [self addSubview:secondLineView];

        // 返回按钮
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelButton setTitle:@"返回" forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        cancelButton.frame = CGRectMake(0, pickerView.bottom, self.width/2, 30);
        [self addSubview:cancelButton];
        self.cancelButton = cancelButton;
        
        // 确认按钮
        UIButton *conformButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [conformButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [conformButton setTitle:@"确认" forState:UIControlStateNormal];
        [conformButton addTarget:self action:@selector(conformAction) forControlEvents:UIControlEventTouchUpInside];
        conformButton.frame = CGRectMake(self.width/2, pickerView.bottom, self.width/2, 30);
        [self addSubview:conformButton];
        self.conformButton = conformButton;
    }
    return self;
}

#pragma mark 取消操作
- (void)cancelAction {
    if (self.cancelActionBlock) {
        self.cancelActionBlock();
    }
}

#pragma mark 确认操作
- (void)conformAction {
    if (self.conformActionBlock) {
        self.conformActionBlock(_selectDateString);
    }
}

- (void)createMonthListWithYear:(NSInteger)year {
    NSString *yearString = [NSString stringWithFormat:@"%ld", (long)year];
    self.monthList = _dataSourceDictionary[yearString];
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (_dataSourceIsDictionaryOrArray == kDictionary) {
        return 2;
    } else {
        return 1;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (_dataSourceIsDictionaryOrArray == kDictionary) {
        if (0 == component) {
            return _yearList.count;
        } else if (1 == component) {
            // 需要创建日数组-懒加载
            [self createMonthListWithYear:self.selectedYear];
            return self.monthList.count;
        }
    } else {
        return _dataSourceArray.count;
    }
    return 0;
}

//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    if (_dataSourceIsDictionaryOrArray == kDictionary) {
//        if (0 == component) {
//            return _yearList[row];
//        } else if (1 == component) {
//            return _monthList[row];
//        }
//    } else {
//        return _dataSourceArray[row];
//    }
//    return nil;
//}

//每个地方要显示的文字
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *textlabel = [[UILabel alloc] init];
    textlabel.textAlignment = NSTextAlignmentCenter;
    
    if (_dataSourceIsDictionaryOrArray == kDictionary) {
        if (component == 0) {
            textlabel.text = [NSString stringWithFormat:@"%@年", _yearList[row]];
        } else {
            textlabel.text = [NSString stringWithFormat:@"%@月", _monthList[row]];
        }
    } else {
        textlabel.text = [NSString stringWithFormat:@"%@年", _dataSourceArray[row]];
    }
    return textlabel;
}


#pragma mark UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (_dataSourceIsDictionaryOrArray == kDictionary) {
        self.selectedYear = [pickerView selectedRowInComponent:0]+kBeginYear;
        if (component == 0) {
            [pickerView reloadComponent:1];
        }
        int row1 = (int)[pickerView selectedRowInComponent:1];
        if (row1 > 8) {
            _selectDateString = [NSString stringWithFormat:@"%ld-%ld-01", (long)self.selectedYear, (long)(row1+1)];
        } else {
            _selectDateString = [NSString stringWithFormat:@"%ld-0%ld-01", (long)self.selectedYear, (long)(row1+1)];
        }
    } else {
        _selectDateString = [NSString stringWithFormat:@"%@-01-01", _dataSourceArray[row]];
    }
}

#pragma mark setter
- (void)setDataSourceDictionary:(NSDictionary *)dataSourceDictionary {
    _dataSourceDictionary = dataSourceDictionary;
    _dataSourceArray = nil;
    
    _yearList = [self sortDataYearCongXiaoDaoDa:dataSourceDictionary.allKeys];
    NSString *tempLastYearString = _yearList.lastObject;
    _selectedYear = tempLastYearString.intValue;
    // 让pickerView默认选择最后的年月
    [self.pickerView selectRow:(_yearList.count-1) inComponent:0 animated:YES];
    
    // 默认初始值
    NSArray *monthArray = dataSourceDictionary[tempLastYearString];
    NSString *tempLastMonthString = monthArray.lastObject;
    [self.pickerView selectRow:monthArray.count-1 inComponent:1 animated:YES];
    if (tempLastMonthString.intValue > 9) {
        _selectDateString = [NSString stringWithFormat:@"%@-%@-01", tempLastYearString, tempLastMonthString];
    } else {
        _selectDateString = [NSString stringWithFormat:@"%@-0%@-01", tempLastYearString, tempLastMonthString];
    }
    [self.pickerView reloadAllComponents];
}

- (void)setDataSourceArray:(NSArray *)dataSourceArray {
    _dataSourceArray = dataSourceArray;
    _dataSourceDictionary = nil;
    // 让pickerView默认选择最后的年月
    [self.pickerView selectRow:(dataSourceArray.count-1) inComponent:0 animated:YES];
    
    // 默认初始值
    _selectDateString = [NSString stringWithFormat:@"%@-01-01", dataSourceArray.lastObject];
    [self.pickerView reloadAllComponents];
}

#pragma mark 让年份从小到大排序
#pragma mark 按时间从高到低
- (NSArray *)sortDataYearCongXiaoDaoDa:(NSArray *)dataArray {
    NSArray *sortedArray = [dataArray sortedArrayUsingComparator: ^NSComparisonResult(NSString *yearString1, NSString *yearString2) {
        return [yearString1 compare: yearString2];
    }];
    
    return [sortedArray copy];
}

@end
